package Easy;
//21.合并两个有序链表
class ListNode {
    int val;
    ListNode next;

    ListNode() {
    }

    ListNode(int val) {
        this.val = val;
    }

    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}

public class Solution21 {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if(l1==null&&l2==null)
            return null;
        ListNode l3 = new ListNode();
        ListNode p = l3;
        while (l1 != null && l2 != null) {
            if (l1.val < l2.val) {
                p.val = l1.val;
                l1 = l1.next;
            } else {
                p.val = l2.val;
                l2 = l2.next;
            }
            p.next = new ListNode();
            p = p.next;
        }
        while (l1 != null) {
            p.val = l1.val;
            if (l1.next != null)
                p.next = new ListNode();
            l1=l1.next;
            p=p.next;
        }
        while (l2 != null) {
            p.val = l2.val;
            if (l2.next != null)
                p.next = new ListNode();
            l2=l2.next;
            p=p.next;
        }
        return l3;
    }

    public static void main(String[] args) {
        Solution21 s21 = new Solution21();
        ListNode result = s21.mergeTwoLists(new ListNode(-9, new ListNode(3)), null);
        while (result!=null){
            System.out.print(result.val+" ");result=result.next;
        }
    }
}
